//
//  PTStuLifeServiceMyReleaseCell.m
//  PlanTech
//
//  Created by ruifeng song on 2018/7/11.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "PTStuLifeServiceMyReleaseCell.h"
#import "XHStarRateView.h"
#import "SINACalendarUtils.h"
#import "PTCoreDefine.h"

@interface PTStuLifeServiceMyReleaseCell()

@property(nonatomic,strong)XHStarRateView *starRateView;
@property(nonatomic,strong)UIButton * secondButton;
@property(nonatomic,strong)UILabel * ratingNameLabel;

@end

@implementation PTStuLifeServiceMyReleaseCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, ScreenWidth, 184)];
        backView.backgroundColor =[UIColor whiteColor];
        [self.contentView addSubview:backView];
        
        _headerImageView =[[UIImageView alloc]initWithFrame:CGRectMake(15, 7, 34, 34)];
        _headerImageView.backgroundColor = [UIColor redColor];
        _headerImageView.layer.cornerRadius = 17;
        _headerImageView.layer.masksToBounds = YES;
        [backView addSubview:_headerImageView];
        
        _userNameLabel= [[UILabel alloc]initWithFrame:CGRectMake(63,16, ScreenWidth - 63, 14)];
//        _userNameLabel.text = @"宋佳瑛（快递服务）";
        _userNameLabel.textColor = PTColor_2e2e2e;
        _userNameLabel.font = PTFONT_BOLD_15;
        [backView addSubview:_userNameLabel];
        
       _callPhoneButton= [UIButton buttonWithType:UIButtonTypeCustom];
        [_callPhoneButton setFrame:CGRectMake(ScreenWidth -50-12- 30, 9, 30, 30)];
        [_callPhoneButton setImage:[UIImage imageNamed:@"call_phone_img"] forState:UIControlStateNormal];
        _callPhoneButton.hidden = YES;
        [_callPhoneButton addTarget:self action:@selector(onClickCallPhoneButton:) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:_callPhoneButton];
        
        _statusLabel= [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth -50-12,16, 50, 14)];
//        _statusLabel.text = @"待接单";
        _statusLabel.textColor = PTColor_2e2e2e;
        _statusLabel.textAlignment = NSTextAlignmentRight;
        _statusLabel.font = PTFONT_15;
        [backView addSubview:_statusLabel];
        
        UILabel * lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(63, 48, ScreenWidth - 63, .5)];
        lineLabel.backgroundColor = PTColor_e0e0e0;
        [backView addSubview:lineLabel];
    
        _receiveGoodsLabel= [[UILabel alloc]initWithFrame:CGRectMake(63, lineLabel.bottom + 15,ScreenWidth - 63, 12)];
//        _receiveGoodsLabel.text = @"收货时间：12:00-14:00";
        _receiveGoodsLabel.font = PTFONT_12;
        _receiveGoodsLabel.textColor = PTColor_2e2e2e;
        [backView addSubview:_receiveGoodsLabel];
        
        _serviceContentLabel= [[UILabel alloc]initWithFrame:CGRectMake(63, _receiveGoodsLabel.bottom +7, ScreenWidth - 63, 12)];
//        _serviceContentLabel.text = @"快递点：山西大学东门";
        _serviceContentLabel.font = PTFONT_12;
        _serviceContentLabel.textColor = PTColor_2e2e2e;
        [backView addSubview:_serviceContentLabel];
        
        _userAddressLabel= [[UILabel alloc]initWithFrame:CGRectMake(63, _serviceContentLabel.bottom + 7, ScreenWidth - 63, 12)];
//        _userAddressLabel.text = @"收货地址：山西师范大学女士宿舍多少教室多...";
        _userAddressLabel.font = PTFONT_12;
        _userAddressLabel.textColor = PTColor_2e2e2e;
        [backView addSubview:_userAddressLabel];
        
        _feeLabel= [[UILabel alloc]initWithFrame:CGRectMake(63,_userAddressLabel.bottom +7, ScreenWidth - 63, 12)];
//        _feeLabel.text = @"酬金：3RMB";
        _feeLabel.textColor = PTColor_2e2e2e;
        _feeLabel.font = PTFONT_12;
        [backView addSubview:_feeLabel];
        
        _commendView = [[UIView alloc]initWithFrame:CGRectMake(63, _feeLabel.bottom +7, backView.width - 63, 12)];
        _commendView.hidden = YES;
        [backView addSubview:_commendView];
        
        _ratingNameLabel= [[UILabel alloc]initWithFrame:CGRectMake(0,0,38, 12)];
        _ratingNameLabel.text = @"评分：";
        _ratingNameLabel.textColor = PTColor_2e2e2e;
        _ratingNameLabel.font = PTFONT_12;
        [_commendView addSubview:_ratingNameLabel];
        
        _starRateView = [[XHStarRateView alloc]initWithFrame:CGRectMake(_ratingNameLabel.right, 1, 58, 10) numberOfStars:5 selectNumberStars:0 rateStyle:WholeStar isAnination:YES isTouch:NO finish:^(CGFloat currentScore) {
            
        }];
        [_commendView addSubview:_starRateView];
        
        _firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_firstButton setFrame:CGRectMake(ScreenWidth - 12 - 76, 149, 76, 25)];
        _firstButton.layer.cornerRadius = 12;
        _firstButton.layer.masksToBounds = YES;
        [_firstButton setTitle:@"取消订单" forState:UIControlStateNormal];
        [_firstButton setTitleColor:PTColor_2e2e2e forState:UIControlStateNormal];
        _firstButton.titleLabel.font = PTFONT_12;
        _firstButton.hidden = YES;
        [_firstButton addTarget:self action:@selector(onClickFirstButton:) forControlEvents:UIControlEventTouchUpInside];
        _firstButton.layer.borderWidth = 1;
        _firstButton.layer.borderColor = PTColor_349ef7.CGColor;
        [backView addSubview:_firstButton];
        
        _secondButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_secondButton setFrame:CGRectMake(ScreenWidth - 12 - 76 - 10 - 76, 149, 76, 25)];
        _secondButton.layer.cornerRadius = 12;
        _secondButton.layer.masksToBounds = YES;
        [_secondButton setTitle:@"举报" forState:UIControlStateNormal];
        [_secondButton setTitleColor:PTColor_2e2e2e forState:UIControlStateNormal];
        _secondButton.hidden = YES;
        [_secondButton addTarget:self action:@selector(onClickSecondButton:) forControlEvents:UIControlEventTouchUpInside];
        _secondButton.titleLabel.font = PTFONT_12;
        _secondButton.layer.borderWidth = 1;
        _secondButton.layer.borderColor = PTColor_ff3a31.CGColor;
        [backView addSubview:_secondButton];
        
    }
    return self;
}

-(void)updateServiceMyReleaseCellWith:(LifeServiceMyListModel *)listModel
{
    SINAUserModel *userModel = [[SINACommonUserHelper share]getUserModel];
    
    self.serviceUUID = listModel.serviceUUID;
    
    if (userModel.userTitleImageUrl && userModel.userTitleImageUrl.length > 0) {
        
        [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:userModel.userTitleImageUrl] placeholderImage:[UIImage imageNamed:@"list_header_default_img"]];
    }
    else
    {
        [self.headerImageView setImage:[UIImage imageNamed:@"list_header_default_img"]];
    }

    NSString * serviceType = @"";
    NSString * serviceContentStr = @"";
    NSString * userAddressStr = @"";
    NSString * serviceName = @"";
    NSString * serviceAddress = @"";
    
    switch ([listModel.type integerValue]) {
        case PTLifeService_ExpressType:
            serviceType = @"代取快递";
            serviceContentStr = @"快递点：";
            userAddressStr = @"收货地址：";
            serviceName = listModel.destinationAddress;
            serviceAddress = listModel.userAddress;
            break;
        case PTLifeService_PrintType:
        {
            serviceType = @"打印复印";
            serviceContentStr = @"打印份数：";
            userAddressStr = @"打印地点：";
            if (listModel.copies ==nil) {
                listModel.copies = [NSNumber numberWithInteger:0];
            }
            serviceName = [NSString stringWithFormat:@"%@",listModel.copies];
            serviceAddress = listModel.destinationAddress;
        }
            break;
        case PTLifeService_BuyMealType:
            serviceType = @"捎饭带饭";
            serviceContentStr = @"餐厅名称：";
            userAddressStr = @"餐厅地址：";
            serviceName = listModel.destinationName;
            serviceAddress = listModel.destinationAddress;
            break;
        case PTLifeService_HotboomType:
            serviceType = @"超市代购";
            serviceContentStr = @"超市名称：";
            userAddressStr = @"超市地点：";
            serviceName = listModel.destinationName;
            serviceAddress = listModel.destinationAddress;
            break;
    }
    //快递点
    self.serviceContentLabel.text = [NSString stringWithFormat:@"%@%@",serviceContentStr,serviceName];
    
    //收货地址
    self.userAddressLabel.text = [NSString stringWithFormat:@"%@%@",userAddressStr,serviceAddress];
    
    self.userNameLabel.text = [NSString stringWithFormat:@"%@（%@）",userModel.userRealName,serviceType];

    switch ([listModel.status integerValue]) {
        case PTLifeService_NormalStatus:
        {
            //判断是否已经过期
            if ([[SINACalendarUtils currentTimeStr] doubleValue]>[listModel.endTime doubleValue]) {
                
                //已过期
                self.statusLabel.text = @"已过期";
                self.callPhoneButton.hidden= YES;
                self.secondButton.hidden = NO;
                self.firstButton.hidden = NO;
                
                [self.firstButton setTitle:@"重新发布" forState:UIControlStateNormal];
                [self.secondButton setTitle:@"已过期" forState:UIControlStateNormal];
                self.secondButton.tag = MyReleaseOrder_TimeOutType;
                self.firstButton.tag = MyReleaseOrder_ResetType;
                
                self.firstButton.layer.borderWidth = 1;
                self.firstButton.layer.borderColor = PTColor_349ef7.CGColor;
                self.secondButton.layer.borderWidth = 1;
                self.secondButton.layer.borderColor = PTColor_ff3a31.CGColor;
            }
            else
            {
                self.statusLabel.text = @"待接单";
                self.callPhoneButton.hidden =YES;
            
                self.secondButton.hidden = YES;
                self.firstButton.hidden = NO;
                [self.firstButton setTitle:@"取消订单" forState:UIControlStateNormal];
                self.firstButton.layer.borderWidth = 1;
                self.firstButton.tag = MyReleaseOrder_CancelType;
                self.firstButton.layer.borderColor = PTColor_349ef7.CGColor;
            }

        }
            break;
        case PTLifeService_CancelReleaseStatus:
        {
            self.statusLabel.text = @"已取消";
            self.callPhoneButton.hidden= YES;
            self.secondButton.hidden = NO;
            self.firstButton.hidden = NO;
            
            [self.firstButton setTitle:@"重新发布" forState:UIControlStateNormal];
            [self.secondButton setTitle:@"删除订单" forState:UIControlStateNormal];
            self.secondButton.tag = MyReleaseOrder_DeleteType;
            self.firstButton.tag = MyReleaseOrder_ResetType;
        
            self.firstButton.layer.borderWidth = 1;
            self.firstButton.layer.borderColor = PTColor_349ef7.CGColor;
            self.secondButton.layer.borderWidth = 1;
            self.secondButton.layer.borderColor = PTColor_ff3a31.CGColor;
            
        }
            break;
        case PTLifeService_TakeOrderUnDoneStatus:
        {
             self.statusLabel.text = @"已接单";
            self.callPhoneButton.hidden = NO;
            self.firstButton.hidden = YES;
            self.secondButton.hidden = YES;
            
            self.phoneNum = listModel.receiver.phone;

        }
            break;
        case PTLifeService_TakeOrderDoneStatus:
        {
             self.statusLabel.text = @"待完成";
            self.callPhoneButton.hidden = NO;
            
            self.firstButton.hidden = NO;
            self.secondButton.hidden = NO;
            [self.firstButton setTitle:@"完成" forState:UIControlStateNormal];
            [self.secondButton setTitle:@"举报" forState:UIControlStateNormal];
            self.firstButton.tag = MyReleaseOrder_FinishType;
            self.secondButton.tag = MyReleaseOrder_ReprotType;
            
            self.firstButton.layer.borderWidth = 1;
            self.firstButton.layer.borderColor = PTColor_349ef7.CGColor;
            self.secondButton.layer.borderWidth = 1;
            self.secondButton.layer.borderColor = PTColor_ff3a31.CGColor;
            
            self.phoneNum = listModel.receiver.phone;

        }
            break;
        case PTLifeService_FinishStatus:
        {
             self.statusLabel.text = @"待评价";
            self.callPhoneButton.hidden = NO;
           
            self.secondButton.hidden = YES;
            self.firstButton.hidden = NO;
            [self.firstButton setTitle:@"评价" forState:UIControlStateNormal];
            self.firstButton.layer.borderWidth = 1;
            self.firstButton.tag = MyReleaseOrder_CommentType;
            
            self.firstButton.layer.borderColor = PTColor_349ef7.CGColor;
            
            self.phoneNum = listModel.receiver.phone;
        }
            break;
        case PTLifeService_CommentStatus:
        {
            self.statusLabel.text = @"已评价";
            self.callPhoneButton.hidden = NO;
            
            self.secondButton.hidden = YES;
            
            self.firstButton.hidden = NO;
            [self.firstButton setTitle:@"删除订单" forState:UIControlStateNormal];
            self.firstButton.layer.borderWidth = 1;
            self.firstButton.tag = MyReleaseOrder_DeleteType;
            self.firstButton.layer.borderColor = PTColor_ff3a31.CGColor;
            
            self.phoneNum = listModel.receiver.phone;

        }
            break;
        case PTLifeService_WaitSolveStatus:
        {
            self.statusLabel.text = @"待处理";
            self.callPhoneButton.hidden = NO;
            
            self.secondButton.hidden = YES;
            
            self.firstButton.hidden = NO;
            [self.firstButton setTitle:@"已举报" forState:UIControlStateNormal];
            self.firstButton.layer.borderWidth = 1;
            self.firstButton.tag = MyReleaseOrder_ReportFinishType;
            
            self.firstButton.layer.borderColor = PTColor_ff3a31.CGColor;
            
            self.phoneNum = listModel.receiver.phone;
        
        }
            
            break;
    }
    
    if ([listModel.status integerValue] == PTLifeService_CommentStatus) {
        self.commendView.hidden = NO;
        
        CGFloat starNum = 0;
        
        if (listModel.rating !=nil) {
            
            starNum = [listModel.rating floatValue];
        }
        
        [self.starRateView setCurrentScore:starNum];
    }
    else
    {
        self.commendView.hidden = YES;
    }
    
    //收货时间
    NSString *startimeStr=[NSString stringWithFormat:@"%@",listModel.startTime];
    NSString *endTimeStr=[NSString stringWithFormat:@"%@",listModel.endTime];
    
    self.receiveGoodsLabel.text = [NSString stringWithFormat:@"收货时间：%@-%@",[SINACalendarUtils getDateStringWithTimeStr:startimeStr andFormat:@"MM月dd日 HH:mm"],[SINACalendarUtils getDateStringWithTimeStr:endTimeStr andFormat:@"HH:mm"]];
    
     self.feeLabel.text = [NSString stringWithFormat:@"酬金：%@RMB",listModel.reward];
    
}

-(void)onClickCallPhoneButton:(id)sender
{
    NSMutableString * string = [[NSMutableString alloc] initWithFormat:@"tel:%@",self.phoneNum];
    
    WISAlertView * alertView = [[WISAlertView alloc]initWithTitle:@"拨打电话" message:string sureBtn:@"确定" cancleBtn:@"取消"];
  
    alertView.resultIndex= ^(NSInteger index) {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
    };
    [alertView showXLAlertView];
    
}

-(void)onClickFirstButton:(UIButton *)sender
{
    //完成、取消订单、已举报、评价、删除订单、重新发布
    if (sender.tag == MyReleaseOrder_FinishType) {
        
        [self.delegate onClickFinishOrderItem:self];
    }
    else if (sender.tag == MyReleaseOrder_CancelType)
    {
        [self.delegate onClickCancelOrderItem:self];
    }
    else if (sender.tag == MyReleaseOrder_ReportFinishType)
    {
        //只显示，不做任何操作
    }
    else if (sender.tag == MyReleaseOrder_CommentType)
    {
        [self.delegate onClickCommentsOrderItem:self];
    }
    else if (sender.tag == MyReleaseOrder_DeleteType)
    {
        //取消订单后，显示的删除订单按钮
        [self.delegate onClickDeleteOrderItem:self];
    }
    else if (sender.tag == MyReleaseOrder_ResetType)
    {
        [self.delegate onClickResetReleaseOrderItem:self];
    }
}

-(void)onClickSecondButton:(UIButton *)sender
{
    //举报 、删除订单
    if (sender.tag == MyReleaseOrder_ReprotType) {
        
        [self.delegate onClickReportUserItem:self];
    }
    else if (sender.tag == MyReleaseOrder_DeleteType)
    {
        //完成订单，删除
        [self.delegate onClickDeleteOrderItem:self];
    }
    else if (sender.tag == MyReleaseOrder_TimeOutType)
    {
        
    }
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
